//
//  DirectMCell.m
//  Megafon
//
//  Created by teng jianzhao on 12-2-9.
//  Copyright 2012 广州市易杰数码科技有限公司. All rights reserved.
//

#import "DirectMCell.h"
#import <QuartzCore/QuartzCore.h>
#import "MessageCell.h"
#import "BusinessManager.h"
#import "ThirdAccountManager.h"
#import "BaseFriendManager.h"
#import "ThirdAccountInfo.h"
#import "RCSUtils.h"

@implementation DirectMCell

@synthesize dirtMsg;

- (id)init{
	
	self = [super init];
	if (self) {
	}
	return self;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
	self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	if (self) {
		//发送时间
		//		sendTime = [[UIView alloc] initWithFrame:CGRectZero];
		//		[self addSubview:sendTime];
		
		//左气泡 右气泡
		bgView = [[BgImageView alloc] init];
		bgView.userInteractionEnabled = YES;
		[self.contentView addSubview:bgView];
		
		//显示发送或接收的文字
		showText = [[UILabel alloc] init];
		showText.lineBreakMode   = UILineBreakModeWordWrap;
		showText.numberOfLines   = 0;
		showText.backgroundColor = [UIColor clearColor];
		showText.font            = [UIFont systemFontOfSize:ShowTextFontD];
		[showText sizeToFit];
		[bgView addSubview:showText];
		
		//头像的阴影
		shadowBG = [[UIView alloc] initWithFrame:CGRectMake(10, 5.0f, 48, 48)];
		shadowBG.layer.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.45].CGColor;
		shadowBG.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
		shadowBG.layer.shadowOpacity = 1.0f;
		shadowBG.layer.shouldRasterize = NO;
		shadowBG.layer.shadowRadius = 2;
		[self.contentView addSubview:shadowBG];
		
		//头像
		headImage = [[AsyncImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];
		headImage.layer.cornerRadius = 4.0;
		headImage.layer.masksToBounds = YES;
		[shadowBG addSubview:headImage];
		
		sendStateView = [[UIImageView alloc] init];
		sendStateView.userInteractionEnabled = YES;
		[self.contentView addSubview:sendStateView];
	}
	return self;
}

- (void)resetObjects
{
	showText.frame = CGRectZero;
	showText.text = @"";
	
	bgView.frame = CGRectZero;
	bgView.image = nil;

	sendStateView.frame = CGRectZero;
	sendStateView.image = nil;
}

- (void)initCellWithDirtMsg:(DirectMsgData *)dMsg
{
	if (dMsg == nil) 
	{
		return;
	}
	isRecv = NO;
	ThirdAccountInfo *twAccount = [[BusinessManager sharedManager].thirdAccManager thirdAccByType:11];
	if (twAccount ==nil) 
	{
		return;
	}
	if ([dMsg.toUserId isEqualToString:twAccount.account]) {
		isRecv = YES;
	}
	dirtMsg = dMsg;
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	if (!isRecv && [dMsg.sendState intValue]==2) {
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setStateViewRect) name:@"Twitter_Send_Direct_Message" object:dMsg]; 
	}
	[self resetObjects];
}

//设置发送状态图标
- (void)setStateViewRect{
	if (dirtMsg.sendState != nil && [dirtMsg.sendState intValue] == 3) {
		CGRect rect;
		rect.size = CGSizeMake(19, 19);
		rect.origin.x = bgView.frame.origin.x - rect.size.width - 5;
		rect.origin.y = bgView.frame.origin.y + (bgView.frame.size.height - rect.size.height)/2;
		sendStateView.image = [UIImage imageNamed:@"MessageSendFail@2x.png"];
		sendStateView.frame = rect;
	}
	else if(dirtMsg.sendState != nil && [dirtMsg.sendState intValue] == 2) {
		//发送中
		
	}else if(dirtMsg.sendState != nil && [dirtMsg.sendState intValue] == 1){
		CGRect rect;
		rect.size = CGSizeMake(19, 19);
		rect.origin.x = bgView.frame.origin.x - rect.size.width - 5;
		rect.origin.y = bgView.frame.origin.y + (bgView.frame.size.height - rect.size.height)/2;
		sendStateView.image = [UIImage imageNamed:@"onebox_ic_sendsucceed"];//变成发送成功的图标
		sendStateView.frame = rect;
	}
}


- (void)loadHeadImageData
{
	[headImage initSelfImgView:[RCSUtils defaultPortraitImage]];
	BaseFriendManager *bfMgr = [BusinessManager sharedManager].baseFriendManager;
	BaseFriend *aFriend = [bfMgr thirdFriendByID:dirtMsg.fromUserId andType:[dirtMsg.snsType intValue]];
	if (aFriend != nil) {//从好友列表中找
		NSString *headPath = [RCSUtils GetSNSCachedImageFilePath:[NSString stringWithFormat:@"%@%@.png",aFriend.trdFriendType,aFriend.userAccount]];
		UIImage *img = [UIImage imageWithContentsOfFile:headPath];
		if (img) {
			[headImage SetUIImage:img];
		}
	}
}

- (CGRect)createShowObjectRect:(CGSize)aSize{
	
	CGRect rect1 = bgView.frame;
	CGRect retRect;
	retRect.size = aSize ;
	if (isRecv) {
		retRect.origin.x = CuspW + (rect1.size.width - CuspW - aSize.width)/2;
	}
	else {
		retRect.origin.x = (rect1.size.width - CuspW - aSize.width)/2 - 2;
	}
	retRect.origin.y = (rect1.size.height - aSize.height)/2;
	return retRect;
}

- (CGSize)calMsgTextHeight:(NSString *)str {
	
	CGSize textSize = {220.0, 20000.0};
	CGSize size     = [str sizeWithFont:[UIFont systemFontOfSize:ShowTextFontD] 
					  constrainedToSize:textSize];
	
	return size;
}


- (CGFloat)calCellHeight{
	DirectMsgData *message = dirtMsg;
	CGSize asize;
    asize.width = 0.0;
    asize.height = 0.0;
	
	asize = [self calMsgTextHeight:message.dmMessage];
	
	CGFloat cellH = 0.0;
	cellH += 11 * 2;
	cellH += asize.height;
	return cellH;
}

- (CGRect)createSelfViewRect{
	
	CGRect retRect;
	retRect.origin.x = 0;
	retRect.origin.y = 0;
	retRect.size.width = 320;
	
	CGRect rect1 = bgView.frame;
	
	retRect.size.height = rect1.size.height  + rect1.origin.y;
	retRect.size.height += 11;
	if (retRect.size.height <= 45+22) {
		retRect.size.height = 67;
	}
	return retRect;
}

- (CGRect)createBgViewRect:(CGSize)aSize{
	
	CGRect retRect;
	retRect.size.width = aSize.width + 22;
	
	if (retRect.size.width <= 45) {//最小宽度为45
		retRect.size.width = 45;
	}
	if (retRect.size.width > 260) {//最大宽度为260
		retRect.size.width = 260;
	}
	retRect.size.width += CuspW;
	
	retRect.size.height = aSize.height + 28;
	if (retRect.size.height <= 45) {//最小高度为35
		retRect.size.height = 45;
	}
	
	retRect.origin.y = 11;
	//DDLogVerbose(@"%f",retRect.origin.y);
	if (isRecv) {
		CGRect headRect = shadowBG.frame;
		retRect.origin.x = headRect.origin.x + headRect.size.width + 6;
	}
	else {
		retRect.origin.x = 320-10-retRect.size.width;
	}

	return retRect;
}

- (void)refreshViewWithSize:(CGSize)aSize{
	//先计算"左气泡"或者"右气泡"的坐标
	bgView.frame = [self createBgViewRect:aSize];
	if (isRecv) {
		if([headImage superview] == nil)
		{
			[shadowBG addSubview:headImage];
		}
		[self loadHeadImageData];
		bgView.image = [[UIImage imageNamed:@"bg_chat_u.png"] stretchableImageWithLeftCapWidth:26 topCapHeight:24];
//        showText.textColor = [UIColor blackColor];
		showText.textColor = [UIColor colorWithRed:114.0/255.0f green:137.0/255.0f blue:81.0/255.0f alpha:1.0f];
//		if (_parent && [_parent isKindOfClass:[AllOfOneFriendController class]]) {
//			AllOfOneFriendController *pCtl = (AllOfOneFriendController *)_parent;
//			headImage.image = [pCtl getHeadImageFromContact];
//		}
//		else {
//			headImage.image = [RCSUtils defaultPortraitImage];
//		}
	}
	else {
		if([headImage superview] != nil)
		{
			[headImage removeFromSuperview];
		}
		[self setStateViewRect];
		bgView.image = [[UIImage imageNamed:@"bg_chat_me.png"] stretchableImageWithLeftCapWidth:26 topCapHeight:24];
		showText.textColor = [UIColor colorWithRed:51.0/255.0f green:51.0/255.0f blue:51.0/255.0f alpha:1.0f];
 //       showText.textColor = [UIColor blackColor];
	}
	
	//调整要显示的内容的坐标
	showText.frame = [self createShowObjectRect:aSize];
	//调整头像的坐标
//	[self createHeadImageRect];
	
	//调整定时发送的消息时显示的"时钟"和"时间"坐标
//	[self createSendOnTimeViewRect];
//	[self createWarningViewRect];
	//调整整个CELL的FRAME
	self.frame = [self createSelfViewRect];
	
}


- (void)refreshCurrentCellView{
	DirectMsgData *message = dirtMsg;
	showText.text = dirtMsg.dmMessage;
	
	CGSize txtSize = [self calMsgTextHeight:message.dmMessage];
	[self refreshViewWithSize:txtSize];
}

- (void)dealloc
{
	[showText release];
	[headImage release];	        //头像
	[bgView release];			//左气泡 右气泡
	
	[shadowBG release];				//头像的阴影
	
	[sendStateView release];
	[super dealloc];
}


@end
